package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("下单成功~");
    }

    //http://localhost:8080/order/page?
    // page=1&pageSize=10&number=56464&beginTime=2023-03-01 00:00:00&endTime=2023-03-28 23:59:59

    /**
     * 订单详情
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper();
        //添加过滤条件
        lqw.like(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        if (StringUtils.isNotEmpty(beginTime)) {
            lqw.ge(Orders::getOrderTime, beginTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            lqw.le(Orders::getOrderTime, endTime);
        }

        //添加排序
        lqw.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo, lqw);

        return R.success(pageInfo);
    }

    /**
     * 修改订单状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> status(@RequestBody Orders orders) {
        orderService.updateById(orders);
        return R.success("修改成功~");
    }
}
